#include "StdAfx.h"
#include "YGoodsTabCmd.h"
#include "..\DataLayer\YGoodsTab.h"

YGoodsTabCmd::YGoodsTabCmd(void)
{
}

YGoodsTabCmd::~YGoodsTabCmd(void)
{
}

BOOL YGoodsTabCmd::Add(YGoodsTab* pYGoodsTab)
{
	CString strSql = _T("");
	strSql.Format(_T("select * from ys_goods"));
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	if ( !pRecordset->IsEof() )
	{
		bRet = pRecordset->AddNew();
		if ( bRet )
		{
			CString strCode(pYGoodsTab->GetCode());
			CString strName(pYGoodsTab->GetName());
			CString strBarcode(pYGoodsTab->GetBarcode());
			strCode = SqlFilterToDb(strCode);
			strName = SqlFilterToDb(strName);
			strBarcode = SqlFilterToDb(strBarcode);

			pRecordset->SetFieldValue( _T("ygs_code"),strCode);
			pRecordset->SetFieldValue( _T("ygs_name"),strName);
			pRecordset->SetFieldValue( _T("ygs_barcode"),strBarcode);
			pRecordset->SetFieldValue( _T("ys_ygt_id"),pYGoodsTab->GetSortId());
			bRet = pRecordset->Update();
			if ( bRet )
			{
				int nId = -1;
				pRecordset->GetFieldValue( _T("ygs_id"),nId );
				pYGoodsTab->SetId(nId);
			}
		}
	}
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
	return bRet;
}

BOOL YGoodsTabCmd::DelByID(int nID)
{
	CString strSql = _T("");
	strSql.Format(_T("select * from ys_goods where ygs_id=%d"),nID);
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	if ( !pRecordset->IsEof() )
	{
		bRet = pRecordset->Delete();
	}
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
	return bRet;
}

BOOL YGoodsTabCmd::EditByID(YGoodsTab* pYGoodsTab)
{
	CString strSql = _T("");
	strSql.Format(_T("select * from ys_goods where ygs_id=%d"),pYGoodsTab->GetId());
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	if ( !pRecordset->IsEof() )
	{
		pRecordset->Edit();
		CString strCode(pYGoodsTab->GetCode());
		CString strName(pYGoodsTab->GetName());
		CString strBarcode(pYGoodsTab->GetBarcode());
		strCode = SqlFilterToDb(strCode);
		strName = SqlFilterToDb(strName);
		strBarcode = SqlFilterToDb(strBarcode);

		pRecordset->SetFieldValue( _T("ygs_code"),strCode);
		pRecordset->SetFieldValue( _T("ygs_name"),strName);
		pRecordset->SetFieldValue( _T("ygs_barcode"),strBarcode);
		pRecordset->SetFieldValue( _T("ys_ygt_id"),pYGoodsTab->GetSortId());
		bRet = pRecordset->Update();
	}
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
	return bRet;
}

void YGoodsTabCmd::GetByID(YGoodsTab* pYGoodsTab)
{
	CString strSql = _T("");
	strSql.Format(_T("select * from ys_goods where ygs_id=%d"),pYGoodsTab->GetId());
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	if ( !pRecordset->IsEof() )
	{
		int nId = -1;
		CString strCode = _T("");
		CString strName = _T("");
		CString strBarcode = _T("");
		int nSortId = -1;
		pRecordset->GetFieldValue( _T("ygs_id"),nId);
		pRecordset->GetFieldValue( _T("ygs_code"),strCode);
		pRecordset->GetFieldValue( _T("ygs_name"),strName);
		pRecordset->GetFieldValue( _T("ygs_barcode"),strBarcode);
		pRecordset->GetFieldValue( _T("ys_ygt_id"),nSortId);
		strCode = SqlFilterToStr(strCode);
		strName = SqlFilterToStr(strName);
		strBarcode = SqlFilterToStr(strBarcode);

		pYGoodsTab->SetId(nId);
		pYGoodsTab->SetCode( (LPTSTR)(LPCTSTR)strCode );
		pYGoodsTab->SetName( (LPTSTR)(LPCTSTR)strName );
		pYGoodsTab->SetBarcode( (LPTSTR)(LPCTSTR)strBarcode );
		pYGoodsTab->SetSortId( nSortId );
	}
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
}

void YGoodsTabCmd::Get(CTypedPtrList<CPtrList, YGoodsTab *>*pRecList)
{
	CString strSql = _T("");
	strSql.Format(_T("select * from ys_goods"));
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	while ( !pRecordset->IsEof() )
	{
		YGoodsTab* pYGoodsTab = new YGoodsTab;
		int nId = -1;
		CString strCode = _T("");
		CString strName = _T("");
		CString strBarcode = _T("");
		int nSortId = -1;
		pRecordset->GetFieldValue( _T("ygs_id"),nId);
		pRecordset->GetFieldValue( _T("ygs_code"),strCode);
		pRecordset->GetFieldValue( _T("ygs_name"),strName);
		pRecordset->GetFieldValue( _T("ygs_barcode"),strBarcode);
		pRecordset->GetFieldValue( _T("ys_ygt_id"),nSortId);
		strCode = SqlFilterToStr(strCode);
		strName = SqlFilterToStr(strName);
		strBarcode = SqlFilterToStr(strBarcode);

		pYGoodsTab->SetId(nId);
		pYGoodsTab->SetCode( (LPTSTR)(LPCTSTR)strCode );
		pYGoodsTab->SetName( (LPTSTR)(LPCTSTR)strName );
		pYGoodsTab->SetBarcode( (LPTSTR)(LPCTSTR)strBarcode );
		pYGoodsTab->SetSortId( nSortId );
		pRecList->AddTail(pYGoodsTab);

		pRecordset->MoveNext();
	}
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
}